package com.kaiwukj.android.ufamily.mvp.ui.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.kaiwukj.android.ufamily.R;

public class AmountJJLayout extends FrameLayout {

    private int mCount;
    private TextView etCount;
    private int minCount = 1;
    private int maxCount = 99;

    public AmountJJLayout(@NonNull Context context) {
        this(context, null);
    }

    public AmountJJLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AmountJJLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public AmountJJLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.widget_amount_jj, this);
        etCount = view.findViewById(R.id.et_count);
        TextView tvJian = view.findViewById(R.id.tv_jian);
        TextView tvJia = view.findViewById(R.id.tv_jia);

        mCount = Integer.parseInt(etCount.getText().toString().trim());
        tvJia.setOnClickListener(v -> {
                    if (mCount < maxCount) {
                        etCount.setText(String.valueOf(++mCount));
                        if (mListener != null)
                            mListener.onCountChanged(mCount > 0 ? mCount : minCount);
                    }
                }
        );
        tvJian.setOnClickListener(v ->
        {
            if (mCount > minCount) {
                etCount.setText(String.valueOf(--mCount));
                if (mListener != null)
                    mListener.onCountChanged(mCount > 0 ? mCount : minCount);
            }
        });

        /*etCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    mCount = Integer.parseInt(s.toString());
                } catch (NumberFormatException e) {
                    mCount = 0;
                }
                if (mListener != null)
                    mListener.onCountChanged(mCount > 0 ? mCount : minCount);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });*/

    }

    public int getCount() {
        return mCount;
    }

    public void setCount(int count) {
        mCount = count;
        if (etCount != null) etCount.setText(String.valueOf(count));
    }

    private OnCountChangedListener mListener;

    public void setOnCountChangedListener(OnCountChangedListener listener) {
        mListener = listener;
    }

    public interface OnCountChangedListener {
        void onCountChanged(int count);
    }
}
